function getVowList(){
    var api = "/backstage/vows/list";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200) {
                var vows = data.data;
                $("#vowDataTable").DataTable({
                    data: vows,
                    destroy: true,
                    columnDefs: [
                        {
                            "targets": "vowID",
                            "data": "id",
                        },
                        {
                            "targets": "vowName",
                            "data": "name",
                        },
                        {
                            "targets": "vowContent",
                            "data": "content",
                        },
                        {
                            "targets": "vowOperation",
                            "defaultContent": 
                            '<button class="btn btn-primary btn-xs vowDetail">编辑</button>' +
                            '<button class="btn btn-danger btn-xs vowDelete">删除</button>',
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    })
}

function getVowDetail(vowID){
    var api = "/backstage/vows/" + vowID;
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200) {
                var vow = data.data;
                $("#nameInput").val(vow.name);
                $("#contentInput").val(vow.content);
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    })
}

function deleteVow(vowID){
    var api = "/backstage/vows/" + vowID;
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: "DELETE",
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status == 200) {
                console.log("请求成功");
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    })
}

function initVowList(){
    getVowList();
    $('#vowDataTable').on('click', 'button', function () {
        var table = $("#vowDataTable").DataTable();
        var vowID = table.row($(this).parents('tr')).data().id;
        var actionName = $(this)[0].classList[3];

        if(actionName == "vowDelete"){
            var r = confirm("是否删除发愿文？");
            if (r == true){
                deleteVow(vowID);
                getVowList();
            }
        }
        else if(actionName == "vowDetail"){
            var href = "vow_detail.html" + "?vowID=" + vowID + "&mode=update";
            window.location.href = href;
        }

    } );
}

function createVow(name, content){
    var api = "/backstage/vows/list";
    var data = {
        "name": name,
        "content": content,
    };
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: "POST",
        data: data,
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status == 200) {
                console.log("请求成功");
                var href = "vow_list.html";
                window.location.href = href;
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    });
}

function updateVow(id, name, content){
    var api = "/backstage/vows/" + id;
    var data = {
        "name": name,
        "content": content,
    };
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: "PUT",
        data: data,
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status == 200) {
                console.log("请求成功");
                var href = "vow_list.html";
                window.location.href = href;
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    });

}


function initVowDetail() {
    var vowID = getQueryString("vowID");
    var mode = getQueryString("mode");

    if(mode == "update"){
        getVowDetail(vowID);
        var isCreate = false;
    }
    else{
        var isCreate = true;
    }

    $('#saveVowBtn').on('click', function () {
        var name = $("#nameInput").val();
        var content = $("#contentInput").val();
        console.log(isCreate)
        if(isCreate){
            createVow(name, content);
        }
        else{
            updateVow(vowID, name, content);
        }
    });
}

function init() {
    var parts = window.location.pathname.split("/");
    var lastPart = parts[parts.length - 1];

    var initFuncMap = {
        "vow_list.html": initVowList,
        "vow_detail.html": initVowDetail,
    };

    var initFunc = initFuncMap[lastPart];
    initFunc();
}

$(function(){
    init();
})